package com.rzjm.api.netty.water.business.inhandler;

import cn.hutool.core.util.StrUtil;
import com.rzjm.api.entity.DeviceWaterMessages;
import com.rzjm.api.netty.water.CommandSender;
import com.rzjm.api.netty.water.ResponseData;
import com.rzjm.api.netty.water.entity.ReceiveData;
import com.rzjm.api.util.DateUtils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 * @author WangWT
 * @Description: 处理上报数据业务逻辑
 * @date 2023/4/14 9:23
 */
interface InHandler {
	Logger LOGGER = LoggerFactory.getLogger(InHandler.class);

	/**
	 * 处理数据
	 */
	void handler(ChannelHandlerContext ctx, ReceiveData receiveData);

	/**
	 * 上报数据日志对象获取
	 *
	 * @param receiveData
	 */
	default DeviceWaterMessages getMessage(ReceiveData receiveData) {
		DeviceWaterMessages deviceMessages = null;
		deviceMessages = new DeviceWaterMessages();
		Date nowDate = DateUtils.getNowDate();
		deviceMessages.setCreatedAt(nowDate);
		deviceMessages.setUpdatedAt(nowDate);
		deviceMessages.setDeviceId(receiveData.getId());
		deviceMessages.setType(receiveData.getType());
		deviceMessages.setV1(StrUtil.toStringOrNull(receiveData.getV1()));
		deviceMessages.setV2(StrUtil.toStringOrNull(receiveData.getV2()));
		deviceMessages.setV3(StrUtil.toStringOrNull(receiveData.getV3()));
		deviceMessages.setV4(StrUtil.toStringOrNull(receiveData.getV4()));
		deviceMessages.setV5(StrUtil.toStringOrNull(receiveData.getV5()));
		deviceMessages.setV6(StrUtil.toStringOrNull(receiveData.getV6()));
		deviceMessages.setV7(StrUtil.toStringOrNull(receiveData.getV7()));
		deviceMessages.setV8(StrUtil.toStringOrNull(receiveData.getV8()));
		deviceMessages.setV9(StrUtil.toStringOrNull(receiveData.getV9()));
		deviceMessages.setV10(StrUtil.toStringOrNull(receiveData.getV10()));
		deviceMessages.setV11(StrUtil.toStringOrNull(receiveData.getV11()));
		deviceMessages.setCreatedAt(nowDate);
		deviceMessages.setUpdatedAt(nowDate);
		return deviceMessages;
	}

	/**
	 * 回复设备消息
	 *
	 * @param replyData
	 * @param deviceId
	 */
	default void reply(String replyData, Integer deviceId) {
		// 回复设备消息
		if (StringUtils.isNotEmpty(replyData) && deviceId != null) {
			ResponseData responseData = CommandSender.sentAsyncCommand(deviceId, replyData);
			LOGGER.info("=====>回复设备消息结果 code={}, msg={}", responseData.getCode(), responseData.getMsg());
		}
	}

	/**
	 * 回复设备消息
	 *
	 * @param replyData
	 * @param deviceId
	 */
	default void reply(byte[] replyData, Integer deviceId) {
		// 回复设备消息
		if (replyData.length > 0 && deviceId != null) {
			// 将字节数组包装成ByteBuf
			ByteBuf byteBuf = Unpooled.buffer(replyData.length);
			byteBuf.writeBytes(replyData);
			ResponseData responseData = CommandSender.sentAsyncCommand(deviceId, byteBuf);
			LOGGER.info("=====>回复设备消息结果 code={}, msg={}", responseData.getCode(), responseData.getMsg());
		}
	}

	default double judgePositiveAndNegative(int number, Integer multiple) {
		double result = number;
		if (number > 20000) {
			result = ~(65531 - number);
		}
		if (multiple != null) {
			result = (double) number / multiple;
		}
		return result;
	}


}
